package com.baosight.risk.service.fltd.impl;

import com.baosight.risk.api.fltd.AuthoTypeService;
import com.baosight.risk.api.xtzy.IXtzyUserService;
import com.baosight.risk.common.constant.WorkflowConstant;
import com.baosight.risk.service.fltd.vo.FlaaNodeAuthoTypeVO;
import com.baosight.risk.service.fltd.vo.FlaaNodeAuthoVO;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指定人  specailUserRole
 */
@Slf4j
@Service("specailUserRole")
public class SpecailUserRoleAuthoTemplate implements AuthoTypeService {

    @Autowired
    private AuthoTypeTemplate authoTypeService;

    @Autowired
    private IXtzyUserService xtzyUserService;

    @Override
    public Map<String, Object> parse(String template, Map<String, Object> param) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        FlaaNodeAuthoTypeVO authInfo = (FlaaNodeAuthoTypeVO) param.get("authInfo");
        //流程参与者
        List<XtzyUser> userList = (List<XtzyUser>) param.get("userList");

        try {
            List<XtzyUser> newUsers = new ArrayList<>();
            XtzyUser xtzyUser = xtzyUserService.selectByName(authInfo.getNodeAuth());
            newUsers.add(xtzyUser);
            authoTypeService.unionUsers(userList, newUsers);
        } catch (Exception e) {
            throw new Exception("根据指定人授权关系查找审批人异常！");
        }
        return resultMap;
    }
}
